package servlet;

import java.io.IOException;
import java.util.List;

import javax.ejb.EJB;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dominio.SolicitudCompra;
import bean.remote.SolicitudFabricaRemote;

public class SolicitudesCompraServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2663315807460183277L;

	@EJB(beanName = "solicitudFabricaBean")
	private SolicitudFabricaRemote solicitudFabricaRemote;
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		List<SolicitudCompra> all = solicitudFabricaRemote.findAll();
		
		String page = "solicitudesCompra.jsp";

		req.setAttribute("data", all);
		
		// Disptching request
		RequestDispatcher dispatcher = req.getRequestDispatcher(page);

		if (dispatcher != null) {
			dispatcher.forward(req, resp);
			return;
		}
		return;
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String[] codigos = req.getParameterValues("seleccionado");
		for (int i = 0; i < codigos.length; i++) {
			String codigo = codigos[i];
			SolicitudCompra solicitudFabrica = solicitudFabricaRemote.findByCodigo(Long.valueOf(codigo));
			solicitudFabricaRemote.fabricar(solicitudFabrica);
		}
	}
}
